package zsc.gof.dao.luence;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import zsc.gof.dao.PremisesDao;
import zsc.gof.entity.Premises;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"/config/application*.xml"})
public class IndexManager {
	@Autowired
	PremisesDao dao;
	@Test public void createIndex() throws IOException {
		Map map = new HashMap<>();
		List<Premises> list = dao.search(map);
		List<Document> docList = new ArrayList<Document>();
		System.out.println(list.size());
		Document document = null;
		for(Premises p:list){
			document = new Document();
			Field id = new StringField("id", p.getBuildId()+"", Store.YES);
			Field address = new TextField("address", p.getBuildAddress(),Store.YES);
			Field build = new TextField("build", p.getBuildName(),Store.YES);
			Field devel = new TextField("devel", p.getDeveloper(),Store.YES);
			
			document.add(address);
			document.add(build);
			document.add(devel);
			document.add(id);
			docList.add(document);
		}
		// 分析文档，对文档中的field域进行分词
		Analyzer analyzer = new StandardAnalyzer();
		// 创建索引
		// a) 创建索引库目录
		Directory directory = FSDirectory.open(new File("A:\\index"));
		// b) 创建IndexWriterConfig对象
		IndexWriterConfig cfg = new IndexWriterConfig(Version.LATEST, analyzer);
		// c) 创建IndexWriter对象
		IndexWriter writer = new IndexWriter(directory, cfg);
		// d) 通过IndexWriter对象添加文档对象（document）
		for (Document doc : docList) {
			writer.addDocument(doc);
		}
		// f) 关闭IndexWriter
		writer.close();
	}
	@Test public void delete() throws IOException{
		// 分析文档，对文档中的field域进行分词
		Analyzer analyzer = new StandardAnalyzer();
		// a) 创建索引库目录
		Directory directory = FSDirectory.open(new File("A:\\index"));
		// b) 创建IndexWriterConfig对象
		IndexWriterConfig cfg = new IndexWriterConfig(Version.LUCENE_4_10_3, analyzer);
		//创建indexWriter
		IndexWriter indexWriter = new IndexWriter(directory, cfg);
		
		//indexWriter.deleteDocuments(new Term("id","220"));
		//删除全部记录
		indexWriter.deleteAll();
		indexWriter.close();
	}
	@Test public void update() throws IOException{
		Analyzer analyzer = new StandardAnalyzer();
		// a) 创建索引库目录
		Directory directory = FSDirectory.open(new File("A:\\index"));
		// b) 创建IndexWriterConfig对象
		IndexWriterConfig cfg = new IndexWriterConfig(Version.LUCENE_4_10_3, analyzer);
		//创建indexWriter
		IndexWriter indexWriter = new IndexWriter(directory, cfg);
		
		//指定查询条件
		Document doc = new Document();
		// 修改后的对象
		doc.add(new TextField("address", "guangdong",Store.YES));
		
		indexWriter.updateDocument(new Term("address","anhui"), doc);
		
		indexWriter.close();
		
	}
	
}
